package com.cell.api.application.netty.handler;

import com.cell.api.application.netty.handler.base.IBaseHandler;
import com.cell.api.application.netty.utils.ByteUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;

/**
 * <p>
 * 设备状态
 * </p>
 *
 * @author zdy
 * @date 2021/11/10
 */
public class DeviceStatusHandler extends IBaseHandler {

    /**
     * 开始解析集中器dtu发送过来数据域
     *
     * @param dataLength 数据域长度
     * @param bytes      请求数据域
     */
    @Override
    public void doRequest(int dataLength, byte[] bytes) {
        //byteBuf= 0600ffff190002fe01000000
        ByteBuf byteBuf = Unpooled.copiedBuffer(bytes);
        System.out.println("数据解码内容:" + ByteBufUtil.hexDump(byteBuf));
        //跳过-备用4个字节
        byteBuf.skipBytes(4);
        //byteBuf= 190002fe01000000
        //读取设备ID-2个字节
        byte[] deviceBytes = new byte[2];
        byteBuf.readBytes(deviceBytes);
        int deviceId = ByteUtils.getLowHighByteInt(deviceBytes);
        System.out.println(deviceId);
        //跳过-备用1个字节
        byteBuf.skipBytes(1);
        //byteBuf= fe01000000
        //设备变量-1个字节
        byte deviceVarByte = byteBuf.readByte();
        int deviceVar = ByteUtils.getInt(deviceVarByte);
        System.out.println(deviceVar);
        //byteBuf= 01000000
        //设备状态-1个字节
        byte deviceStatusByte = byteBuf.readByte();
        int deviceStatus = ByteUtils.getInt(deviceStatusByte);
        System.out.println(deviceStatus);
    }

    /**
     * 处理响应
     *
     * @return 响应内容
     */
    @Override
    public byte[] doResponse() {
        return null;
    }
}
